import 'package:dio/dio.dart';

import '../entity/book_entity.dart';
import '../entity/book_info_entity.dart';
import '../entity/borrow_again_entity.dart';
import '../entity/borrow_entity.dart';
import '../entity/face_search_entity.dart';
import '../entity/reader_entity.dart';
import '../entity/return_entity.dart';
import '../main.dart';
import '../util/toast_util.dart';
import 'api_url.dart';
import 'http_helper.dart';

Future<BookEntity?> findBook(bookNo) async {

  var response = await HttpHelper.instance.sip2Post(findBookUrl, data: {'device_id': settingEntity.deviceNo, 'book_no': bookNo});

  if(response['code'] == 200){
    var data  = BookEntity.fromJson(response['result']);
    return data;
  }else{
    toastWarn("${response['message']}");
    return null;
  }
}

Future<BookEntity?> searchBook(BookEntity book) async {
  var (status,response) = await HttpHelper.instance.post(searchBookUrl, data: {'isbn': book.isbn, 'limit': 1, 'include cover':true}, options:  Options(headers: {"app_key":appKey}));
  if(status == 200){
    var data  = BookInfoEntity.fromJson(response);
    var bookInfo = data.list;
    if(bookInfo!=null && bookInfo.isNotEmpty){
      var cover = data.list![0].coverBase64;
      if(cover != null){
        var coverList = cover.split(',');
        if(coverList .isNotEmpty && coverList.length>=2){
          book.cover = data.list![0].coverBase64!.split(',')[1];
        }
      }
      book.summary = data.list![0].summary;
    }
    return book;
  }else{
    toastWarn('$response');
    return null;
  }

}

Future<ReaderEntity?> findReader(readerNo) async {
  var response = await HttpHelper.instance.sip2Post(findReaderUrl, data: {'device_id': settingEntity.deviceNo, 'reader_no': readerNo,'password':'000000','category':''});

  if(response['code'] == 200){
    var data  = ReaderEntity.fromJson(response['result']);
    return data;
  }else{
    toastWarn("${response['message']}");
    return null;
  }

}


Future<BorrowEntity?> bookBorrow(readerNo,bookNo) async {
  var response = await HttpHelper.instance.sip2Post(borrowUrl, data: {'device_id': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo});

  if(response['code'] == 200){
    var data  = BorrowEntity.fromJson(response['result']);
    return data;
  }else {
    toastWarn("${response['message']}");
    return null;
  }

}

Future<BorrowEntity?> bookBorrowCommon(readerNo,bookNo,realReaderNo) async {
  var response = await HttpHelper.instance.sip2Post(borrowUrl, data: {'device_id': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo,'trans_remark':"原读者号借书: $realReaderNo"});

  if(response['code'] == 200){
    var data  = BorrowEntity.fromJson(response['result']);
    return data;
  }else {
    toastWarn("${response['message']}");
    return null;
  }

}

Future<ReturnEntity?> bookReturn(bookNo) async {
  var response = await HttpHelper.instance.sip2Post(returnUrl, data: {'device_id': settingEntity.deviceNo,'book_no':bookNo});

  if(response['code'] == 200){
    var data  = ReturnEntity.fromJson(response['result']);
    return data;
  }else {
    toastWarn("${response['message']}");
    return null;
  }

}

Future bookBorrowAgain(readerNo,bookNo) async {
  var response = await HttpHelper.instance.sip2Post(borrowAgainUrl, data: {'device_id': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo});

  if(response['code'] == 200){
    var data  = BorrowAgainEntity.fromJson(response['result']);
    return (data, null);
  }else {
    return (null, response['message']);
  }
}

Future faceAdd(userId,userName,img) async {
  var (status,response) = await HttpHelper.instance.post(faceAddUrl, data: {'user_id': userId, 'user_name': userName,'image_base64':img});
  if(status == 200){
    return (true,null);
  }else if(status == 500){
    return (false, response);
  }else {
    return (false, response['error']);
  }

}
Future faceSearch(img) async {
  var (status,response) = await HttpHelper.instance.post(faceSearchUrl, data: {'image_base64':img});
  if(status == 200){
    var data  = FaceSearchEntity.fromJson(response);
    return (data,null);
  }else if(status == 500){
    return (false, response);
  }else{
    return (null, response['error']);
  }

}
Future<bool> faceQuery(userId) async {
  var (status,response) = await HttpHelper.instance.post(faceQueryUrl, data: {
    "user_id": userId}
  );
  if(status == 200){
    bool isEmpty  = response['empty'];
    return !isEmpty;
  }else{
    toastWarn('${response['error']}');
    return false;
  }

}
